﻿#ifndef __slt_logimpl_20200309_H
#define __slt_logimpl_20200309_H
#include <stdio.h>
#include <stdarg.h>
#include <string.h>
#include <deque>
#include <list>
#include <mutex>
#include <memory>
#include <thread>

#ifdef WIN32
#include <functional>
#else
#include <unistd.h>
#endif

#include "SLT_timer.h"
#include "SLT_log.h"
//#ifndef MAX_PATH
//#define MAX_PATH 260
//#endif

struct LoggerInfo
{
    void clear() {
        _filesize = -1;
        _filecount = -1;
        _level = SLT_UNKNOW;
        _display = -1;
        _sync_display_out = -1;
    }
    std::string _key;
    std::string _logname;    //日志文件名称
    std::string _path;      //日志输出目录
    int _filesize = 0;
    int _filecount = 0;

    int  _level = SLT_WARM;
    int _display = 1;          //0-fasle,1-true
    int _sync_display_out = 0; //0-fasle,1-true
};
class CLogHelper
{
public:
    struct loggerhead
    {
        bool  _disp;
        int  _len;   //不包括\0的长度
        char  _data[1];
    };
    
public:
    CLogHelper();
    ~CLogHelper();

    bool config(const std::string& configPath, bool isUpdate);
    int init();
    void uninit();
    void writeLog(loggerhead* plogger);
    
    void setLogPath(const char* _logPath) { m_logPath = _logPath; }
    void setLogFilePrefix(const char* _filePrefix) { m_logPrefixName = _filePrefix; }
    void setLogLimitSize(uint32_t  _size_MB) { m_limitSizeB = _size_MB * 1024 * 1024; }
    void setLogFileCount(uint32_t  _fileCount) { m_filecount = _fileCount-1; }
    void setLogLevel(int _id, int _level);
    void setLogDisplay(int _id, int _disp);
    void setLogDisplaySync(int _id, int _sync);

private:
    std::string openAndRead(const char* path);
    bool configFromStringImpl(std::string content, bool isUpdate);

    bool isDirectory(std::string path);
    void initHistoryLogs(std::string path);

    bool IsFileAlready();
    void Delete_FileCount(int count);
    void procLog();

    //static int CustomFilter(const struct dirent* pDir);
	//void Delete_File(char* cleandir, int days);
    //unsigned long long GetFileSize(const char* filename);
    
    // config file name
    std::string _configFile;
    
public:
    bool        m_init = false;
    bool        m_bconfig = true;        //读取日志配置文件
    FILE* m_fp = nullptr;
    std::string m_logPath = "./log/";    //日志文件路径
    std::string m_logPrefixName;        //创建的日志文件前缀名
    uint32_t    m_limitSizeB = 2 * 1024 * 1024; //日志文件最大尺寸，B
    int         m_filecount = 200;  //日志文件最多保存数量
    LoggerInfo m_loggers[100];
	
    std::mutex m_logListLock;
    std::deque< loggerhead*>  m_logList1;
    std::deque< loggerhead*>  m_logList2;
    std::deque< loggerhead*>* m_plogList = nullptr;

    int         m_fileIndex = 0;

    uint32_t    m_curWriteLen = 0;
    SLT_elapseTimer   m_flushTimer;
    SLT_elapseTimer   m_delFileTimer;

    struct _historyLog_info
    {
        std::string path;
        std::string fileName;
    };
    std::list<_historyLog_info> m_historyLogs;

    std::thread   *m_pwriteThread = nullptr;

};
#endif // LOGHELPER_H
